/*******************************************************************************
 * @project: CDMS_12
 * @package: com.esoon.cdms.ui
 * @file: CdmsUI.java
 * @author: Administrator
 * @created: 2017-6-12
 * @purpose:
 * 
 * @version: 1.0
 * 
 * Revision History at the end of file.
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/

package com.esoon.cdms.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import com.esoon.cdms.decoration.DesignListCellRenderer;
import com.esoon.cdms.menu.action.whereusedinproject.WhereUsedInProjectDialog;
import com.esoon.cdms.model.BuildListListModel;
import com.esoon.cdms.model.BuildListModel;
import com.esoon.cdms.model.DesignListModel;
import com.esoon.cdms.model.DesignModel;
import com.esoon.cdms.model.ProjectListModel;
import com.esoon.cdms.model.ProjectModel;
import com.esoon.cdms.utils.FileUtil;
import com.mentor.chs.api.IXBuildList;
import com.mentor.chs.api.IXDesign;
import com.mentor.chs.api.IXProject;
import com.mentor.chs.plugin.IXApplicationContext;

public class CdmsUI extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4486557176068106110L;
	public static int Width = 1000;
	public static int Height = 700;
	private IXApplicationContext context;
	private IXProject currentProject;
	private List<ProjectModel> projectList;
	private List<DesignModel> designList;
	private List<BuildListModel> buildListList;

	public CdmsUI(IXApplicationContext context) {
		this.context = context;
		initData();
		initUI();
	}

	private void initData() {
		currentProject = context.getCurrentProject();
		// 项目集合
		{

			projectList = new ArrayList<ProjectModel>();
			projectList.add(new ProjectModel(currentProject));
		}

		// BuildList集合
		{
			buildListList = new ArrayList<BuildListModel>();

			Set<IXBuildList> buildLists = currentProject.getBuildLists();
			for (IXBuildList buildList : buildLists) {
				buildListList.add(new BuildListModel(buildList));
			}
		}

		// 设计集合
		{

			designList = new ArrayList<DesignModel>();

			Set<IXDesign> designs = currentProject.getDesigns();
			for (IXDesign design : designs) {
				designList.add(new DesignModel(design));
			}
		}
	}

	/**
	 * 初始化界面
	 */
	private void initUI() {
		JPanel contextPanel = new JPanel();
		this.setContentPane(contextPanel);
		this.setLayout(new BorderLayout());
		this.setTitle("CDMS【12所】");
		this.setModal(true);

		JSplitPane jsp1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		// 菜单
		{
			JMenuBar menuBar = new JMenuBar();
			this.setJMenuBar(menuBar);
			// Action菜单
			{
				// 添加菜单
				JMenu action = new JMenu("Action");
				menuBar.add(action);
				JMenuItem whereUsedInProject = new JMenuItem(
						"WhereUsedInProject");
				whereUsedInProject.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						WhereUsedInProjectDialog ui = new WhereUsedInProjectDialog(CdmsUI.this);
						ui.setVisible(true);
					}
				});
				action.add(whereUsedInProject);
			}
			//Help菜单
			{

				JMenu help = new JMenu("Help");
				menuBar.add(help);
				JMenuItem logicHelp = new JMenuItem("CDMS_12说明");
				logicHelp.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						String notic = FileUtil
								.getStringFromInputStream(getClass()
										.getResourceAsStream("/CDMS_12.txt"));
						JOptionPane.showMessageDialog(CdmsUI.this, notic);
					}
				});
				help.add(logicHelp);
			}
		}
		// 左边显示项目列表
		{
			JScrollPane projectjScrollPane1 = new JScrollPane();
			JPanel projectPanel = new JPanel(new BorderLayout());
			ProjectListModel plm = new ProjectListModel(projectList);
			JList projectList = new JList();
			projectjScrollPane1.setViewportView(projectList);
			projectList.setModel(plm);
			projectList.setCellRenderer(new DesignListCellRenderer());
			// 点击项目列表时触发事件
			projectList.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
				}

			});

			projectPanel.add(projectjScrollPane1, BorderLayout.CENTER);
			jsp1.add(projectPanel, JSplitPane.LEFT);
		}
		// 中间显示当前选中的项目下的设计集合（BuildList)

		JSplitPane jsp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

		{
			JPanel buildListPanel = new JPanel(new BorderLayout());
			BuildListListModel bllm = new BuildListListModel(buildListList);
			JList buildListList = new JList(bllm);
			JScrollPane buildListJScrollPane = new JScrollPane();
			buildListJScrollPane.setViewportView(buildListList);
			buildListList.setCellRenderer(new DesignListCellRenderer());
			buildListPanel.add(buildListJScrollPane, BorderLayout.CENTER);
			jsp2.add(buildListPanel, JSplitPane.LEFT);

		}

		// 右边显示当前选中的项目下的设计列表
		{
			JPanel designPanel = new JPanel(new BorderLayout());
			DesignListModel dlm = new DesignListModel(designList);
			JList designList = new JList(dlm);
			JScrollPane designjScrollPane = new JScrollPane();
			designjScrollPane.setViewportView(designList);
			designList.setCellRenderer(new DesignListCellRenderer());
			designPanel.add(designjScrollPane, BorderLayout.CENTER);
			jsp2.add(designPanel, JSplitPane.RIGHT);

		}

		jsp1.add(jsp2, JSplitPane.RIGHT);
		this.getContentPane().add(jsp1, BorderLayout.CENTER);
		// 设置界面图标
		Toolkit tool = this.getToolkit();
		this.setIconImage(tool
				.getImage("D:/eclipse_wks/CDMS_12/files/logo.ico"));

		// 界面居中
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
		int width = dimension.width;
		int height = dimension.height;
		// 设置窗体可见
		this.setVisible(true);
		// 设置窗体位置和大小
		this.setBounds((width - Width) / 2, (height - Height) / 2, Width,
				Height);

	}
}

/*******************************************************************************
 * <B>Revision History</B><BR>
 * [type 'revision' and press Alt + / to insert revision block]<BR>
 * 
 * 
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/
